package cn.smartorigin.soas.data;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SqliteConnecter {
	
	Connection conn;
	Statement stat;
	ResultSet rs;

	public SqliteConnecter() throws ClassNotFoundException, SQLException{
		Class.forName("org.sqlite.JDBC");
		conn = DriverManager.getConnection("jdbc:sqlite:db/soas.db");
		stat = conn.createStatement();		 
	}
	
	public ResultSet query(String sql) throws SQLException{
		if(sql!=null){
			rs = stat.executeQuery(sql);
			return rs;
		}else{
			return null;
		}

	}
	
	public void insert(String sql) throws SQLException{
		if(sql!=null){
			stat.executeUpdate(sql);
//			conn.commit();
			insertClose();
		}
	}
	
	public void sqliteConnect() throws SQLException{
		conn = DriverManager.getConnection("jdbc:sqlite:db/soas.db");
		stat = conn.createStatement();
	}
		
	public void sqliteClose() throws SQLException{
		if(rs.isClosed()==false){
			rs.close();
		}
		if(conn.isClosed()==false){
			conn.close();
		}
	}
	
	public void insertClose() throws SQLException{
		if(conn.isClosed()==false){
			conn.close();
		}
	}
}
